from django.db import models


#用户表
class User(models.Model):
    username = models.CharField('用户名称', max_length=20)
    password = models.CharField('用户密码', max_length=20)
    user_mobile = models.CharField('用户手机号', max_length=11)
    user_mailbox = models.CharField('用户邮箱', max_length=20)
    logindate = models.DateTimeField('注册日期', auto_now_add=True)
    lastlogin = models.DateTimeField('最后登陆时期', auto_now=True)
    user_id_card = models.CharField('身份号', max_length=18)
    user_image = models.CharField('用户头像', max_length=500)
    user_if = models.BooleanField('用户开关', choices={(0, '有权限'), (1, '没有权限')})

    class Meta():
        db_table = 'user_tb'

    def __str__(self):
        return self.username


#商家关联表
class Shops(models.Model):
    shop_name = models.CharField('店铺', max_length=20)
    shop_image = models.CharField('店铺logo', max_length=500)
    shop_user = models.ForeignKey(User, verbose_name='商家', on_delete=models.CASCADE)
    logindate = models.DateTimeField('注册日期', auto_now_add=True)
    shop_if = models.BooleanField('用户开关', choices={(0, '有权限'), (1, '没有权限')})
    min_price = models.CharField('起送费', max_length=20)

    class Meta():
        db_table = 'shop_tb'

    def __str__(self):
        return self.shop_name


#骑手关联表
class Horseman(models.Model):
    horseman_name = models.ForeignKey(User, verbose_name='骑手', on_delete=models.CASCADE)
    horseman_image = models.CharField('骑手头像', max_length=500)
    logindate = models.DateTimeField('成为骑手的时间', auto_now_add=True)

    class Meta():
        db_table = "horseman_tb"

    def __str__(self):
        return self.horseman_name


#订单表
class Order(models.Model):
    ORDER_STATE = (
        (0, "超时支付失败"),
        (1, "未支付"),
        (2, "已支付"),
        (3, "派送中..."),
        (4, "已完成"),
    )

    PAY_METHOD = (
        (1, "支付宝"),
        (2, "微信"),
    )

    order_id = models.CharField('订单id', max_length=50, primary_key=True)
    logindate = models.DateTimeField('订单生成时间', auto_now_add=True)
    pay_method = models.SmallIntegerField(verbose_name="支付方式", choices=PAY_METHOD)
    pay_state = models.SmallIntegerField(verbose_name="订单状态", choices=ORDER_STATE, default=1)
    sum = models.IntegerField(verbose_name="总数量")
    total_amount = models.DecimalField(verbose_name="总金额", max_digits=7, decimal_places=2)
    remark = models.CharField(verbose_name="备注", max_length=128,null=True)
    shop_order = models.ForeignKey(Shops, verbose_name='商家', on_delete=models.CASCADE)
    user_order = models.ForeignKey(User, verbose_name='用户', on_delete=models.CASCADE)
    horseman_order = models.ForeignKey(Horseman, verbose_name='骑手', on_delete=models.CASCADE)

    class Meta():
        db_table = "order_tb"

    def __str__(self):
        return '订单id:%s-----商家:%s-----用户:%s------骑手:%s'\
               %(self.order_id, self.shop_order, self.user_order, self.horseman_order)


#分类表
class Classify(models.Model):
    classify_name = models.CharField('分类名字', max_length=20)
    shop_claskkify = models.ForeignKey(Shops, on_delete=models.CASCADE, verbose_name='商家id')

    class Meta():
        db_table = 'classify_tb'

    def __str__(self):
        return self.classify_name


#商品表
class Commodity(models.Model):
    commodity_name = models.CharField('商品名', max_length=20)
    price = models.DecimalField(verbose_name="价格", max_digits=7, decimal_places=2)
    commodity_image = models.CharField('商品图片', max_length=500)
    commodity_intro = models.CharField('商品简介', max_length=100)
    commodity_particulars = models.CharField('商品详情', max_length=200)
    commodity_classify = models.ForeignKey(Classify, on_delete=models.CASCADE, verbose_name='分类')
    commodity_shop = models.ForeignKey(Shops, on_delete=models.CASCADE, verbose_name='商品')

    class Meta():
        db_table = 'commodity_tb'

    def __str__(self):
        return self.commodity_name


#地址表
class Address(models.Model):
    address_name = models.CharField('地址', max_length=200)
    coordinate = models.CharField('经纬度', max_length=500)
    label = models.IntegerField('标签')
    address_shop = models.ForeignKey(Shops, on_delete=models.CASCADE, verbose_name='商家地址')

    class Meta():
        db_table = 'address_tb'

    def __str__(self):
        return self.address_name


#套餐表
class Combo(models.Model):
    combo_name = models.CharField('套餐名', max_length=20)

    class Meta():
        db_table = 'combo_tb'

    def __str__(self):
        return self.combo_name


#套餐关联表
class set_meals_dishes(models.Model):
    set_meals_dishes_combo = models.ForeignKey(Combo, on_delete=models.CASCADE, verbose_name='套餐')
    set_meals_dishes_commodity = models.ForeignKey(Commodity, on_delete=models.CASCADE, verbose_name='商品')
    set_meals_dishes_classify = models.ForeignKey(Classify, on_delete=models.CASCADE, verbose_name='分类')

    class Meta():
        db_table = 'set_meals_dishes_tb'

    def __str__(self):
        return self.set_meals_dishes_combo


#口味表
class Taste(models.Model):
    taste_name = models.CharField('口味名字', max_length=20)

    class Meta():
        db_table = 'taste_tb'

    def __str__(self):
        return self.taste_name


#口味关联表
class Taste_commodity(models.Model):
    taste_commodity_taste = models.ForeignKey(Taste, on_delete=models.CASCADE, verbose_name='口味')
    taste_commodity_commodify = models.ForeignKey(Commodity, on_delete=models.CASCADE, verbose_name='菜品')
    the_specific_taste = models.CharField('具体口味', max_length=200)

    class Meta():
        db_table = 'taste_commodity_tb'

    def __str__(self):
        return self.taste_commodity_taste


#员工表
class Staff(models.Model):
    staff_name = models.CharField('员工名字', max_length=20)
    staff_image = models.CharField('员工头像', max_length=500)
    staff_user = models.ForeignKey(User, verbose_name='员工', on_delete=models.CASCADE)
    logindate = models.DateTimeField('注册日期', auto_now_add=True)
    shop_if = models.BooleanField('用户开关', choices={(0, '有权限'), (1, '没有权限')})

    class Meta():
        db_table = 'staff_tb'

    def __str__(self):
        return self.staff_user


#管理员表
class Administrator(models.Model):
    administrator_name = models.CharField('管理员', max_length=20)
    password = models.CharField('管理员密码', max_length=20)
    administrator_mobile = models.CharField('管理员手机号', max_length=11)
    logindate = models.DateTimeField('注册日期', auto_now_add=True)
    lastlogin = models.DateTimeField('最后登陆时期', auto_now=True)
    administrator_image = models.CharField('管理员头像', max_length=500)

    class Meta():
        verbose_name = 'administrator_tb'

    def __str__(self):
        return self.administrator_name